<?php

namespace App\Http\Controllers\Home;
use App\Http\Controllers\Home\LoginController;
use Hash;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class LishiController extends Controller
{     
      //用户的历史记录
      public function getLishi()
      {     
            //判断用户是否登录
            if(empty(session('uid'))){
            return  redirect('Home/login')->with('error','请您登陆');
            }  

            $uid = session('uid');
           
             //调用方法
            $nicheng = $this->cheng();

            //求出用户名下的所有订单
            $res = DB::table('shop_orders')->where('uid',$uid)->get();
            //遍历所有的订单
            $oid = [];
            foreach($res as $k=>$v)
            {

                $oid[] = DB::table('shop_detail')->where('oid',$v->oid)->get();
            }
            
           
            $goods = [];
            //遍历所有商品的gid
            foreach($oid as $k=>$v)
            {
   
               foreach($v as $kk=>$vv)
               { 
                  //把每个订单里的商品压入$goods的空数组中
                   $goods[$v[0]->oid][] = DB::table('shop_goods')->where('gid',$vv->gid)->get();
               }
               
            }
                  
          
            //解析模板
            return view('Home.user.ding',["goods"=>$goods,'nicheng'=>$nicheng]);
      }

      //删除订单数据
      public function getDelete(Request $request)
      {
           $ding = $request->input('ding');
           //把无规律的每十位字符串分割成数组
           $arr[] = str_split($ding,10);

           //数据删除订单
           foreach ($arr as $k=>$v){
             $n = count($v);
             for ($i=0; $i<$n; $i++)
               {
                   // dd($v[$i]);

                   //删除订单主表
                   $res = DB::table('shop_orders')->where('oid',$v[$i])->delete();
                   //删除订单详情表

                   $data = DB::table('shop_detail')->where('oid',$v[$i])->delete();
       
               }
           }
           echo  $data;
          //  dd($arr);
          //  dd(strlen($ding));
          // $a = strlen($ding)/10;
          // $shu = [];
          // for($i=0; $i<$a; $i++)
          // {
          //    //截取字符串
          //    substr($ding,$i*10,($i+1)*9+$i);
          // }
          // $ding = explode();
      }
      

      //个人信息页面
      public function getGeren()
      {     
            //判断用户是否登录
            if(empty(session('uid'))){
            return  redirect('Home/login')->with('error','请您登陆');
            }  
            
            $uid = session('uid');

            //调用方法
            $nicheng = $this->cheng();

            //解析模板
            return view('Home.user.ge',['nicheng'=>$nicheng]);
        

      }

      //添加用户个人资料
      public function postTian(Request $request)
      { 
          // dd($request->all());
          //获取插入数据的字段值
          $data = $request->only(['uname','sex','nicheng','emo','love','_token']);
          $uid = session('uid');
          // dd($uid);
          //调用函数处理头像上传
          // dd($request->input('uface'));
          //判断
          if($request->hasFile('uface')){
            $data['uface'] = $this->upload($request,'uface');
          }
          //执行数据修改
          $res = DB::table('shop_users')->where('uid',$uid)->update($data);
          if($res){
                //成功
                return redirect('/Lishi/xian');
              // echo 1;
          }else{
                return back()->with('error','信息添加失败');
          }
      }

      //封装方法 进行头像上传
      private function upload($request,$filename)
      {
           if($request->hasFile($filename)){
              //获取文件后缀名
              $suffix = $request->file($filename)->getClientOriginalExtension();
              // dd($suffix);
              //随机文件名
              $name = md5(time().rand(1,9999));
              $request->file($filename)->move('./uploads',$name.'.'.$suffix);
              return '/uploads/'.$name.'.'.$suffix;
           }
      }

      //显示用户个人信息
      public function getXian()
      {   
           //在数据库里查询用户的个人信息
           $users = DB::table('shop_users')->where('uid',session('uid'))->first();

           //调用方法
           $nicheng = $this->cheng();
           //解析模板
           return view('Home.user.geindex',['users'=>$users,'nicheng'=>$nicheng]);
      }



      //显示修改密码页
      public function getPass()
      {    
           //判断用户是否登录
           if(empty(session('uid'))){
           return  redirect('Home/login')->with('error','请您登陆');
           }

           //调用方法
           $nicheng = $this->cheng();
           //解析模板
           return view('Home.user.upwd',['nicheng'=>$nicheng]);
      }


      //接受数据处理
      public function postUpwd(Request $request)
      {
          
           //获取表单传过来的密码
           $upwd = $request->input('upwd');
          // dd($upwd);
           //在数据库查询用户密码
           $res = DB::table('shop_users')->where('uid',session('uid'))->select('upwd')->first();
          

           if(Hash::check($upwd, $res->upwd)){

               //新密码
               $mima = $request->input('pass');

               $data = array(
                      'upwd'  =>bcrypt($mima),
               );
            
           
               //执行密码修改
               $a = DB::table('shop_users')->where('uid',session('uid'))->update($data);
               if($a){
                      //成功
                      return redirect('/Home/login');
               }else{
                      return back()->with('error','密码修改失败');
               }
                 
           }else{

               return back()->with('error','原密码错误');
           }



      }


        //求出昵称
        public function cheng()
        {
             //在数据库里查询用户的个人信息
             $users = DB::table('shop_users')->where('uid',session('uid'))->first();
             if(empty($users->nicheng)){
                  return $nicheng = $users->uname;
             }else{

                  return $nicheng = $users->nicheng; 
             }

        }
}